package com.zte.purchase.server.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zte.purchase.dao.IPurchaseDao;
import com.zte.purchase.model.*;
import com.zte.purchase.server.IPurchaseServer;
import com.zte.util.DataComparator;
import com.zte.util.PageModel;
import com.zte.util.TimeComparator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class PurchaseServer implements IPurchaseServer {

    @Autowired
    private IPurchaseDao purchaseDao;

    @Transactional
    @Override
    public boolean addOnePurchase(CPurchaseAndDetails cPad) {
        try {
            purchaseDao.addOnePurchase(cPad);
            cPad.getPurchaseDatails().forEach((details -> {
                purchaseDao.addOneDetails(details);
            }));
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    @Transactional
    @Override
    public boolean deletePurchase(String purchaseId) {
        try {
            purchaseDao.deleteDetails(purchaseId);
            purchaseDao.deleteOnePurchase(purchaseId);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Transactional
    @Override
    public boolean updateOnePurchase(CPurchase cPurchase) {
        return purchaseDao.updateOnePurchase(cPurchase);
    }

    @Override
    public boolean updatePurchaseStatus(CPurchaseAndDetails cPau) {
        return purchaseDao.updatePurchaseAndDetails(cPau);
    }


    @Override
    public List<CPurchase> queryCDetailsByPC(PurchaseCondition conn) {

        List<CPurchase> cPurchases = purchaseDao.queryCDetailsByPC(conn);

        return cPurchases;
    }

    @Transactional
    @Override
    public PageModel<CPurchase> queryPurchasePage(PageModel<CPurchase> pageModel, PurchaseCondition conn) {
        int pageNum = pageModel.getPageNum()<=1?1:pageModel.getPageNum();
        int pageSize = pageModel.getPageSize() <= 0 ? 5 : pageModel.getPageSize();

        PageHelper.startPage(pageNum,pageSize);
        //对conn的数据做处理
        Date[] dates = TimeComparator.changeTime(conn.getPrePurchaseTime(),conn.getAfterPurchaseTime());
        conn.setPrePurchaseTime(dates[0]);
        conn.setAfterPurchaseTime(dates[1]);
        String[] ss = DataComparator.changeSize(conn.getPreTotalMoney(), conn.getAfterTotalMoney());
        conn.setPreTotalMoney(ss[0]);
        conn.setAfterTotalMoney(ss[1]);

        List<CPurchase> cPurchases = purchaseDao.queryCDetailsByPC(conn);

        PageInfo<CPurchase> info = new PageInfo<CPurchase>(cPurchases);

        return PageModel.getPageModel(pageModel,info);
    }

    @Transactional
    @Override
    public CPurchaseAndDetails queryPurchaseDatails(String indent) {
        return purchaseDao.queryPurchaseAndDetails(indent);
    }


    @Override
    public CDetails2 queryDetail2(String pId) {
        CDetails2 cDetails = purchaseDao.queryDetail2(pId);
        return cDetails;
    }

    @Override
    public List<UpdateBrand> queryAllBrands() {
        List<UpdateBrand> brands = purchaseDao.queryAllBrandsU();
        return brands;
    }

    @Override
    public int updatePurppp(UpdataSave updataSave) {
        int i = purchaseDao.updatePurppppp(updataSave);
        return i;
    }

    @Override
    public int updateDettt(UpdataSave updataSave) {
        int i = purchaseDao.updateDettttt(updataSave);
        return i;
    }
}
